package cn.liulin.spring_boot_study.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * cn.liulin.spring_boot_study.condition$
 *
 * @author ll
 * @date 2021-11-01 17:34:43
 **/
public class DatabaseConditional implements Condition {

    /**
     * 数据库装配条件
     * @author ll
     * @date 2021-11-01 17:35:36
     * @param context 条件上下文
     * @param metadata 注释类型的元数据
     * @return boolean true装配Bean，否则不装配
     **/
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //取出环境配置
        Environment environment = context.getEnvironment();
        //判断属性文件是否存在对应的数据库配置
        return environment.containsProperty("database.driverName")
                && environment.containsProperty("database.url")
                && environment.containsProperty("database.username")
                && environment.containsProperty("database.password");
    }
}
